<template>
  <div>
    <a-form-model ref="ruleForm1" :model="form1" :rules="rules1" :label-col="labelCol" :wrapper-col="wrapperCol">
      <p class="title">情报事件信息</p>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="情报事件编号">
            <a-select
              v-model="form1.incidentCode"
              :show-search="true"
              :allowClear="false"
              :default-active-first-option="false"
              :show-arrow="false"
              :filter-option="false"
              :not-found-content="null"
              style="width: 100%"
              placeholder="请输入"
              :disabled="isDisabled2"
              @search="onSearch"
              @change="onChange"
            >
              <a-select-option v-for="(item, key) in bhLit" :key="key" :value="item.incidentId">{{
                item.incidentCode
              }}</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="情报事件名称" prop="incidentName">
            <a-input v-model="form1.incidentName" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="所属单位" required prop="departName">
            <a-input v-model="form1.departName" :disabled="isDisabled1" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="情报事件日期" required prop="rangeTime">
            <a-range-picker
              :allowClear="true"
              v-model="form1.rangeTime"
              :disabled="isDisabled"
              valueFormat="YYYY-MM-DD"
              @change="rangePickerChange1"
              style="width:100%"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="创建人" prop="apprehendUser">
            <a-input v-model="form1.apprehendUser" :disabled="isDisabled1" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="联系电话" prop="apprehendUserPhone">
            <a-input v-model="form1.apprehendUserPhone" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="24">
          <a-form-model-item
            label="说明"
            prop="description"
            :labelCol="{
              sm: { span: 3 }
            }"
            :wrapperCol="{
              sm: { span: 21 }
            }"
          >
            <a-textarea
              v-model="form1.description"
              :disabled="isDisabled"
              placeholder="请输入"
              :auto-size="{ minRows: 4, maxRows: 6 }"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
    <a-form-model ref="ruleForm2" :model="form2" :rules="rules2" :label-col="labelCol" :wrapper-col="wrapperCol">
      <p class="title">情报任务信息</p>
      <a-row>
        <a-col :span="12">
          <a-form-model-item label="情报任务名称" prop="taskName">
            <a-input v-model="form2.taskName" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="情报任务日期" required prop="rangeTime">
            <a-range-picker
              :allowClear="true"
              v-model="form2.rangeTime"
              valueFormat="YYYY-MM-DD"
              @change="rangePickerChange2"
              style="width:100%"
            />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="24">
          <a-form-model-item
            label="情报任务描述"
            prop="description"
            :labelCol="{
              sm: { span: 3 }
            }"
            :wrapperCol="{
              sm: { span: 21 }
            }"
          >
            <a-textarea v-model="form2.description" placeholder="请输入" :auto-size="{ minRows: 4, maxRows: 6 }" />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </div>
</template>
<script>
import * as caseApi from '@/api/wxSmuggle/caseEvent.js'
import { phoneValidata } from '@/utils/validate'
import Vue from 'vue'
import { USER_INFO } from '@/store/mutation-types'
export default {
  components: {},
  data() {
    return {
      labelCol: { span: 5 },
      wrapperCol: { span: 19 },
      form1: {
        incidentId: null,
        incidentName: '',
        departName: '',
        rangeTime: [],
        apprehendUser: '',
        apprehendUserPhone: '',
        description: '',
        category: 2
      },
      form2: {
        rangeTime: []
      },
      rules1: {
        incidentName: [{ required: true, message: '请输入案事件名称' }],
        departName: [{ required: true, message: '请选择所属单位' }],
        rangeTime: [{ required: true, message: '请选择案事件日期' }],
        apprehendUser: [{ required: true, message: '请输入办案人' }],
        apprehendUserPhone: [
          { required: true, message: '请输入联系电话' },
          {
            validator: phoneValidata
          }
        ],
        description: [{ required: true, message: '请输入说明' }]
      },
      rules2: {
        taskName: [{ required: true, message: '请输入任务名称' }],
        rangeTime: [{ required: true, message: '请选择案任务日期' }],
        description: [{ required: true, message: '请输入说明' }]
      },
      isDisabled: false,
      isDisabled1: false,
      isDisabled2: false,
      bhLit: [],
      isValid3: true
    }
  },
  created() {
    let user = Vue.ls.get(USER_INFO)
    this.form1.apprehendUser = Vue.ls.get(USER_INFO).realname
    this.getDepartName(user.orgCode)
    // console.log('1111111111')
    // console.log(this.nowDate())
    // console.log(this.beforeThree())
    //当前日期到后三个月日期
    this.form1.rangeTime = [this.nowDate(), this.beforeThree()]
    this.form2.rangeTime = [this.nowDate(), this.beforeThree()]
    this.form1.startTime = this.nowDate()
    this.form1.endTime = this.beforeThree()
    this.form2.startTime = this.nowDate()
    this.form2.endTime = this.beforeThree()
  },
  methods: {
    /*
     * 获取当前年月日
     * */
    nowDate() {
      const loadYear = new Date().getFullYear()
      let loadMonth = new Date().getMonth() + 1
      let loadDay = new Date().getDate()
      if (loadMonth >= 1 && loadMonth <= 9) {
        loadMonth = '0' + loadMonth
      }
      if (loadDay >= 0 && loadDay <= 9) {
        loadDay = '0' + loadDay
      }
      const date = loadYear + '-' + loadMonth + '-' + loadDay
      return date
    },
    /*
     * 获取后三个月
     * */
    beforeThree() {
      const dates = new Date()
      dates.setMonth(dates.getMonth() + 3)
      var pastMonth = dates.getMonth() + 1
      var pastDay = dates.getDate()
      if (pastMonth >= 1 && pastMonth <= 9) {
        pastMonth = '0' + pastMonth
      }
      if (pastDay >= 0 && pastDay <= 9) {
        pastDay = '0' + pastDay
      }
      const endDate = dates.getFullYear() + '-' + pastMonth + '-' + pastDay
      return endDate
    },
    getDepartName(orgCode) {
      caseApi.getDepartName({ orgCode: orgCode }).then(res => {
        if (res.success) {
          this.form1.departmentId = res.result.id
          this.form1.departName = res.result.departName
          // console.log(this.form1.departmentId)
          // console.log(this.form1.departName)
          this.isDisabled1 = true
        }
      })
    },
    onChange(val) {
      if (val != undefined) {
        caseApi.incidentDetail(val).then(res => {
          if (res.success) {
            this.form1.incidentId = val
            this.form1.incidentCode = res.result.incidentCode
            this.form1.incidentName = res.result.incidentName
            this.form1.departName = res.result.departName
            this.form1.apprehendUser = res.result.apprehendUser
            this.form1.apprehendUserPhone = res.result.apprehendUserPhone
            this.form1.description = res.result.description
            this.form1.rangeTime = [res.result.startTime, res.result.endTime]
            this.form1.startTime = res.result.startTime
            this.form1.endTime = res.result.endTime
            this.isDisabled = true
          }
        })
      } else {
        this.form1 = {
          incidentId: null,
          incidentName: '',
          departName: '',
          rangeTime: [],
          apprehendUser: '',
          apprehendUserPhone: '',
          description: ''
        }
        this.bhLit = []
      }
    },
    onSearch(val) {
      if (val == '') {
        return
      }
      caseApi.queryCodeList({ incidentCode: val, category: 2 }).then(res => {
        if (res.success) {
          this.bhLit = res.result
        }
      })
    },
    rangePickerChange1(date) {
      var oDate1 = new Date(date[0])
      var oDate2 = new Date(this.nowDate())
      //所选时间开始日期不能小于当前日期
      if (oDate1.getTime() < oDate2.getTime()) {
        this.form1.rangeTime = []
        this.$set(this.form1, 'startTime', '')
        this.$set(this.form1, 'endTime', '')
        this.$message.warning('开始日期不能小于当前日期')
        return
      }
      let flag = this.cpDate(date[0], date[1])
      if (flag) {
        this.form1.rangeTime = []
        this.$set(this.form1, 'startTime', '')
        this.$set(this.form1, 'endTime', '')
        this.$message.warning('时间跨度不能超过3个月')
        return
      }
      this.$set(this.form1, 'startTime', date[0])
      this.$set(this.form1, 'endTime', date[1])
    },
    rangePickerChange2(date) {
      var oDate1 = new Date(date[0])
      var oDate2 = new Date(date[1])
      var oDate3 = new Date(this.form1.startTime)
      var oDate4 = new Date(this.form1.endTime)
      this.isValid3 = true
      this.alertText(oDate1, oDate2, oDate3, oDate4)
      this.$set(this.form2, 'startTime', date[0])
      this.$set(this.form2, 'endTime', date[1])
    },
    validateTime() {
      var oDate1 = new Date(this.form2.startTime)
      var oDate2 = new Date(this.form2.endTime)
      var oDate3 = new Date(this.form1.startTime)
      var oDate4 = new Date(this.form1.endTime)
      this.isValid3 = true
      this.alertText(oDate1, oDate2, oDate3, oDate4)
    },
    alertText(oDate1, oDate2, oDate3, oDate4) {
      console.log(oDate1.getTime(), oDate3.getTime())
      //任务开始时间不能小于案事件开始时间
      if (oDate1.getTime() < oDate3.getTime()) {
        this.isValid3 = false
        this.form2.rangeTime = []
        this.$set(this.form2, 'startTime', '')
        this.$set(this.form2, 'endTime', '')
        this.$message.warning('情报任务开始时间不能小于情报事件开始时间')
        return
      }
      console.log(oDate2.getTime(), oDate4.getTime())
      //任务结束时间不能大于案事件结束时间
      if (oDate2.getTime() > oDate4.getTime()) {
        this.isValid3 = false
        this.form2.rangeTime = []
        this.$set(this.form2, 'startTime', '')
        this.$set(this.form2, 'endTime', '')
        this.$message.warning('情报任务结束时间不能大于情报事件结束时间')
        return
      }
    },
    cpDate: function(t1, t2) {
      var beginYear = new Date(t1).getFullYear()
      var beginMonth = new Date(t1).getMonth()
      var beginDate = new Date(t1).getDate()
      var endYear = new Date(t2).getFullYear()
      var endMonth = new Date(t2).getMonth()
      var endDate = new Date(t2).getDate()
      var flag = false
      if (beginYear == endYear) {
        //同年
        if (endMonth - beginMonth > 3) {
          //月间隔超过3个月
          console.log('超过')
          flag = true
        } else if (endMonth - beginMonth == 3) {
          //月间隔为3个月,比较具体日
          if (endDate > beginDate) {
            //结束日期日大于开始日期日
            console.log('超过')
            flag = true
          } else {
            console.log('不超过')
          }
        } else {
          console.log('不超过')
        }
      } else {
        //不同年
        if (endYear - beginYear > 1) {
          console.log('超过')
          flag = true
        } else if (endYear - beginYear == 1) {
          if (beginMonth < 10) {
            //开始年的月份小于10时,不需要跨1年
            console.log('超过')
            flag = true
          } else if (beginMonth + 3 - endMonth < 12) {
            //月间隔大于3个月
            console.log('超过')
            flag = true
          } else if (beginMonth + 3 - endMonth == 12) {
            //月间隔等于3个月,比较具体日
            if (endDate > beginDate) {
              console.log('超过')
              flag = true
            } else {
              console.log('不超过')
            }
          }
        } else {
          console.log('不超过')
        }
      }
      return flag
    },
    onValidate() {
      let isValid1 = false
      let isValid2 = false
      this.$refs.ruleForm1.validate(valid => {
        if (valid) {
          isValid1 = true
        } else {
          isValid1 = false
        }
      })
      this.$refs.ruleForm2.validate(valid => {
        if (valid) {
          isValid2 = true
        } else {
          isValid2 = false
        }
      })
      this.validateTime()
      if (isValid1 && isValid2 && this.isValid3) {
        return true
      } else {
        return false
      }
    },
    getData1() {
      return this.form1
    },
    setData1(obj) {
      this.form1 = obj
      this.$set(this.form1, 'rangeTime', [obj.startTime, obj.endTime])
      this.isDisabled = true
      this.isDisabled2 = true
    },
    getData2() {
      return this.form2
    },
    setData2(obj) {
      this.form2 = obj
      this.$set(this.form2, 'rangeTime', [obj.startTime, obj.endTime])
    },
    disabledDate(current) {
      // Can not select days before today and today
      return current && current > this.moment().endOf('day')
    },

    disabledDateTime() {
      return {
        disabledHours: () => this.range(0, 24).splice(4, 20),
        disabledMinutes: () => this.range(30, 60),
        disabledSeconds: () => [55, 56]
      }
    }
  }
}
</script>
<style scoped lang="less">
.title {
  font-weight: bold;
  // margin-top: 20px;
}
/deep/.ant-form-item {
  margin-bottom: 10px;
}
/deep/.ant-col-sm-3 {
  width: 10.5%;
}
/deep/.ant-col-sm-21 {
  width: 89.5%;
}
</style>
